package com.avalon.shop.service;

import com.avalon.core.exception.AvalonException;
import com.avalon.core.model.RecordRow;
import com.avalon.core.util.JacksonUtil;
import com.avalon.core.util.RestTemplateUtils;
import com.avalon.shop.ShopContext;
import com.avalon.shop.config.ShopConfig;
import com.fasterxml.jackson.core.type.TypeReference;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class UserAddressService {
    private final String serviceName = "pet.address";
    private final ShopConfig shopConfig;
    private final ShopContext shopContext;

    public UserAddressService(ShopConfig shopConfig, ShopContext shopContext) {
        this.shopConfig = shopConfig;
        this.shopContext = shopContext;
    }

    public List<Map> getAddressAll(Integer userId) {
        HashMap<String, Object> param = new HashMap<>();
        param.put("fields", "id,addressDetail,phone,receiverName");
        param.put("rpnCondition", "(=,petUserId," + userId + ")");
        param.put("serviceName", serviceName);
        Map<String, String> headers = new HashMap<>();
        headers.put("content-type", "application/json");
        headers.put("token", shopContext.getBackToken());
        headers.put("Accept", MediaType.toString(Arrays.asList(MediaType.APPLICATION_JSON)));
        ResponseEntity<String> responseEntity = RestTemplateUtils.post(
                shopConfig.getServer() + "/model/get/all",
                headers, param, String.class);
        if (responseEntity.getStatusCode().is2xxSuccessful()) {
            List<Map> maps = JacksonUtil.convert2Object(responseEntity.getBody(), new TypeReference<List<Map>>() {
            });
            return maps;
        } else {
            log.error("getAddressAll 错误:" + responseEntity.getBody());
            throw new AvalonException(responseEntity.getBody());
        }
    }

    public void deleteAddress(Integer id) {
        HashMap<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("serviceName", serviceName);
        Map<String, String> headers = new HashMap<>();
        headers.put("content-type", "application/json");
        headers.put("token", shopContext.getBackToken());
        headers.put("Accept", MediaType.toString(Arrays.asList(MediaType.APPLICATION_JSON)));
        ResponseEntity<String> responseEntity = RestTemplateUtils.post(
                shopConfig.getServer() + "/model/delete",
                headers, param, String.class);
        if (responseEntity.getStatusCode().is2xxSuccessful()) {
        } else {
            log.error("deleteAddress 错误:" + responseEntity.getBody());
            throw new AvalonException(responseEntity.getBody());
        }
    }

    public void addAddress(Integer userId, String addressDetail, String phone, String receiverName) {
        HashMap<String, Object> param = new HashMap<>();
        HashMap<String, Object> value = new HashMap<>();
        value.put("petUserId", userId);
        value.put("addressDetail", addressDetail);
        value.put("phone", phone);
        value.put("receiverName", receiverName);
        param.put("serviceName", serviceName);
        param.put("value", value);
        Map<String, String> headers = new HashMap<>();
        headers.put("content-type", "application/json");
        headers.put("token", shopContext.getBackToken());
        headers.put("Accept", MediaType.toString(Arrays.asList(MediaType.APPLICATION_JSON)));
        ResponseEntity<String> responseEntity = RestTemplateUtils.post(
                shopConfig.getServer() + "/model/add",
                headers, param, String.class);
        if (responseEntity.getStatusCode().is2xxSuccessful()) {
        } else {
            log.error("addAddress 错误:" + responseEntity.getBody());
            throw new AvalonException(responseEntity.getBody());
        }
    }


    public void updateAddress(Integer id, String addressDetail, String phone, String receiverName) {
        HashMap<String, Object> param = new HashMap<>();
        HashMap<String, Object> value = new HashMap<>();
        value.put("addressDetail", addressDetail);
        value.put("phone", phone);
        value.put("receiverName", receiverName);
        value.put("id", id);
        param.put("serviceName", serviceName);
        param.put("value", value);
        Map<String, String> headers = new HashMap<>();
        headers.put("content-type", "application/json");
        headers.put("token", shopContext.getBackToken());
        headers.put("Accept", MediaType.toString(Arrays.asList(MediaType.APPLICATION_JSON)));
        ResponseEntity<String> responseEntity = RestTemplateUtils.post(
                shopConfig.getServer() + "/model/update",
                headers, param, String.class);
        if (responseEntity.getStatusCode().is2xxSuccessful()) {
        } else {
            log.error("updateAddress 错误:" + responseEntity.getBody());
            throw new AvalonException(responseEntity.getBody());
        }
    }

    public RecordRow getAddressOne(Integer id) {
        HashMap<String, Object> param = new HashMap<>();
        param.put("fields", "id,addressDetail,phone,receiverName");
        param.put("serviceName", serviceName);
        param.put("rpnCondition", "(=,id," + id + ")");
        Map<String, String> headers = new HashMap<>();
        headers.put("content-type", "application/json");
        headers.put("token", shopContext.getBackToken());
        headers.put("Accept", MediaType.toString(Arrays.asList(MediaType.APPLICATION_JSON)));
        ResponseEntity<String> responseEntity = RestTemplateUtils.post(
                shopConfig.getServer() + "/model/get/detail",
                headers, param, String.class);
        if (responseEntity.getStatusCode().is2xxSuccessful()) {
            return JacksonUtil.convert2Map(responseEntity.getBody());
        } else {
            log.error("getAddressOne 错误:" + responseEntity.getBody());
            throw new AvalonException(responseEntity.getBody());
        }
    }
}
